package org.inspiration.dudu.web.controller.content;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.inspiration.dudu.common.exception.ServiceException;
import org.inspiration.dudu.common.pojo.Result;
import org.inspiration.dudu.content.model.dto.ArticleDTO;
import org.inspiration.dudu.content.model.vo.ArticleSimpleVO;
import org.inspiration.dudu.content.model.vo.ArticleVO;
import org.inspiration.dudu.web.annotation.Loggable;
import org.inspiration.dudu.web.service.content.ArticleFacadeService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articles")
@Tag(name = "文章接口", description = "提供与文章相关的操作接口")
@RequiredArgsConstructor
@Loggable
public class ArticleController {
    private final ArticleFacadeService articleFacadeService;

    /**
     * 上传文章
     * 传入文章标题、内容和状态
     * 将内容存入文件系统，将文章各种关系存入数据库
     * 返回文章的唯一标识符
     *
     * @param articleDTO 文章数据传输对象
     * @return 返回文章的唯一标识符
     */
    @PostMapping
    @Operation(summary = "上传文章", description = "接收文章标题、内容和状态并存储文章")
    public Result<String> uploadArticle(@RequestBody ArticleDTO articleDTO) {
        String uuid = articleFacadeService.uploadArticle(articleDTO);
        // 返回文章的唯一标识符
        return Result.success(uuid);
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改文章")
    public Result<Void> updateArticle(@RequestBody ArticleDTO articleDTO, @PathVariable("id") String id) {
        articleFacadeService.updateArticle(articleDTO, id);
        return Result.success();
    }

    @PutMapping("/status/{id}")
    @Operation(summary = "修改文章状态")
    public Result<Void> updateArticleStatus(@PathVariable("id") String id, @RequestParam("status") String status) {
        if ((!"DRAFT".equals(status)) && (!"PUBLISHED".equals(status))) {
            throw new ServiceException("非法文章状态");
        }
        articleFacadeService.updateArticleStatus(id, status);
        return Result.success();
    }

    @Operation(summary = "根据ID查询文章", description = "根据文章ID获取文章详细信息")
    @GetMapping("/{id}")
    public Result<ArticleVO> getArticleById(@PathVariable("id") String id) {
        ArticleVO articleVO = articleFacadeService.getArticleById(id);
        return Result.success(articleVO);
    }

    @GetMapping
    @Operation(summary = "获取最近文章列表", description = "分页获取最近的已发布的文章列表")
    public Result<List<ArticleSimpleVO>> getPublishedArticlesOrderByDESC(@RequestParam("pageNum") Integer pageNum,
                                                                         @RequestParam("pageSize") Integer pageSize) {
        List<ArticleSimpleVO> list = articleFacadeService.getPublishedArticlesOrderByDESC(pageNum, pageSize);
        return Result.success(list);
    }

    @GetMapping("/user")
    @Operation(summary = "获取某个用户发布的所有文章", description = "分页且只包含已发布的文章")
    public Result<List<ArticleSimpleVO>> getPublishedArticlesByUserIdOrderByDESC(@RequestParam("userId") Long userId,
                                                                                 @RequestParam("pageNum") Integer pageNum,
                                                                                 @RequestParam("pageSize") Integer pageSize
    ) {
        List<ArticleSimpleVO> list = articleFacadeService.getPublishedArticlesByUserIdOrderByDESC(userId, pageNum, pageSize);
        return Result.success(list);
    }

    @GetMapping("/user/draft")
    @Operation(summary = "获取某个用户未发布的所有文章", description = "分页且只包含未发布的文章")
    public Result<List<ArticleSimpleVO>> getDraftArticlesByUserIdOrderByDESC(@RequestParam("userId") Long userId,
                                                                             @RequestParam("pageNum") Integer pageNum,
                                                                             @RequestParam("pageSize") Integer pageSize) {
        List<ArticleSimpleVO> list = articleFacadeService.getDraftArticlesByUserIdOrderByDESC(userId, pageNum, pageSize);
        return Result.success(list);
    }

    @GetMapping("/user/key-word")
    @Operation(summary = "根据关键词搜索当前用户的文章", description = "分页且同时包含发布和未发布")
    public Result<List<ArticleSimpleVO>> getArticlesByUserIdAndKeyWordOrderByDESC(@RequestParam("userId") Long userId,
                                                                                  @RequestParam("keyWord") String keyWord,
                                                                                  @RequestParam("pageNum") Integer pageNum,
                                                                                  @RequestParam("pageSize") Integer pageSize) {
        List<ArticleSimpleVO> list = articleFacadeService.getArticlesByUserIdAndKeyWordOrderByDESC(userId, keyWord, pageNum, pageSize);

        return Result.success(list);

    }

    @GetMapping("/search")
    @Operation(summary = "根据关键词搜索文章", description = "只包含已发布的文章")
    public Result<List<ArticleSimpleVO>> getPublishedArticlesByKeyWordOrderByDESC(
            @RequestParam("keyWord") String keyWord,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ) {
        List<ArticleSimpleVO> list = articleFacadeService.getPublishedArticlesByKeyWordOrderByDESC(keyWord, pageNum, pageSize);
        return Result.success(list);
    }

}
